﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Windows.Forms.Design.Behavior;

namespace dotNetLab.Widgets.Design
{
   public abstract class VisualPanelDesigner :ParentControlDesigner
    {
        protected Type type_ActionList = null;
        public bool EableUIGlyph = false;
        protected UIGlyph uiGlyph;
        DesignerActionListCollection actionList;
        protected abstract void ProvideType();
        private Adorner myAdorner;
        public Control AddCtrlForParent(Type type_Control)
        {
            IDesignerHost designerHost = (IDesignerHost)this.GetService(typeof(IDesignerHost));
            Control ctrl = (Control)designerHost.CreateComponent( type_Control);
            Control.Parent.Controls.Add(ctrl);
            return ctrl;
        }
       
        public Control AddChildCtrl(Type type_Control)
        {
            IDesignerHost designerHost = (IDesignerHost)this.GetService(typeof(IDesignerHost));
            Control ctrl = (Control)designerHost.CreateComponent(type_Control);
            Control .Controls.Add(ctrl);
            return ctrl;
        }
        public override DesignerActionListCollection ActionLists
        {
            get
            {
                if (actionList == null)
                {
                    /* object [] objArr = new object[1];
                      objArr[0] = this.Component;*/
                    if (type_ActionList == null)
                        ProvideType();
                    actionList = new DesignerActionListCollection();
                    actionList.Add((DesignerActionList)System.Activator.CreateInstance(type_ActionList, this.Component,this));
                }

                return actionList;
            }
        }
        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            PrepareGlyph(component);
            this.ProvideType();
        }
        public EventHandler ClickHandler
        {
            get
            {
                return uiGlyph.uiBehavior.Click;
            }
            set
            {
                uiGlyph.uiBehavior.Click += value;
            }
        }
        protected virtual void PrepareGlyph(IComponent component)
        {
            if (EableUIGlyph)
            {
                myAdorner = new Adorner();
                this.BehaviorService.Adorners.Add(myAdorner);
                this.uiGlyph = new UIGlyph(BehaviorService, (Control)component);
                myAdorner.Glyphs.Add(uiGlyph);
            }
        }
    }
}

/*
 *  //Enable Sub Designable Child Controls 
        // in XXDesigner XX Property Top Add ' [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]'
        //using  '' this.EnableDesignMode(this.Control.WorkingArea, "WorkingArea");''
         public void ApplyChanged(MemberDescriptor m)
        {
            this.RaiseComponentChanged(m,null,null);
        }
        protected virtual void EnableSubDesignableControl()
        {
        }
          public void SetChildDesignable(Control c,string propertyname)
        {
        	this.EnableDesignMode(c,propertyname) ;
        }
     */
/*protected override void Dispose(bool disposing)
      {
          base.Dispose();
          if (type_Glyph != null)
          {
              if (disposing && myAdorner != null)
              {
                  BehaviorService b = BehaviorService;
                  if (b != null)
                  {
                      b.Adorners.Remove(myAdorner);
                  }
              }
          }

      }*/
